package com.example.feidaolumanager.componant

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.feidaolumanager.pojo.User
import com.example.feidaolumanager.viewModel.AreasViewModel

@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun DropdownSelectLevel3Area(
    user: User, areasViewModel: AreasViewModel = viewModel(),
    onSelected: (key: Int) -> Unit,
) {

    var expanded by remember {
        mutableStateOf(false)
    }

    var selectedAreaName by remember {
        mutableStateOf("")
    }

    areasViewModel.getAreas(user, machine = null)
    areasViewModel.getAreas(user)

    TextField(
        value = selectedAreaName, enabled = false,
        onValueChange = {
            selectedAreaName = it
        },
        placeholder = {
            Text(text = "请选择区域")
        },
        modifier = Modifier
            .clickable {
                expanded = true
            }
            .padding(all = 0.dp)
            .fillMaxWidth()
            .height(50.dp)
            .background(color = Color.White),
        trailingIcon = {
            Icon(
                imageVector = Icons.Default.KeyboardArrowDown, contentDescription = "向下的按钮"
            )
        },
        textStyle = TextStyle(color = Color.Black),
        colors = TextFieldDefaults.textFieldColors(
            containerColor = Color.White,
        ),
    )
    Box(modifier = Modifier) {
        DropdownMenu(
            expanded = expanded, onDismissRequest = {
                expanded = false
            }, modifier = Modifier.fillMaxWidth()
        ) {
            areasViewModel.buildingSites.forEach {
                DropdownMenuItem(text = {
                    Text(it.title)
                }, onClick = {
                    selectedAreaName = it.title
                    expanded = false
                    onSelected(it.key)
                })
            }
        }
    }
}
